package com.quyunshuo.module.home.activity.main

import android.util.Log
import androidx.compose.runtime.mutableStateListOf
import androidx.lifecycle.viewModelScope
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.module.home.data.CoffeeProduct
import com.quyunshuo.module.home.room.database.SumDataBase
import com.quyunshuo.module.home.room.entity.PoetEntity
import com.quyunshuo.module.home.room.manager.PoetRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import javax.inject.Inject

/**
 * 首页的VM层
 *
 * @property mRepository HomeRepository 仓库层 通过Hilt注入
 *
 * @author Qu Yunshuo
 * @since 5/25/21 5:41 PM
 */
 @HiltViewModel
class HomeComposeViewModel @Inject constructor(private val mRepository: HomeRepository) : BaseViewModel() {

    private val coffeeList =  CoffeeProduct.coffeeList

    fun getAllList()= coffeeList

    val _coffeeCarList =  mutableStateListOf<CoffeeProduct>()

    fun addCoffeeCar(coffeeProduct: CoffeeProduct) {
        _coffeeCarList.add(coffeeProduct)
    }

    // 计算总价的状态流
    val totalPrice = _coffeeCarList.sumOf { it.price }

    fun getCoffeeListIndex(position:Int) : CoffeeProduct? {
        if (position<0  || position>=coffeeList.size) {
            return null
        }
        return coffeeList[position]
    }





}
/*
compose好乱，有时候viewmodel，使用mutableStateListOf<CoffeeProduct>，
又使用MutableStateFlow<List<CoffeeProduct>>()，我怎么知道呢？什么时候使用哪个？为什么呢？区别在哪里、











 */